package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"short_video/api/request"
	"short_video/global"
	"short_video/internal/dao"
	"short_video/internal/model"
)

func AddDamu(c *gin.Context) {

	var req request.AddDamu

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息失败",
			"data": err.Error(),
		})
		return
	}
	userId := c.GetUint("userId")
	var user model.User
	dao.GetOneById(userId, &user)
	var video model.Video
	if video.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "视频不存在",
			"data": nil,
		})
		return
	}
	tx := global.DB.Begin()

	tx.Updates(model.Video{Id: video.Id, DamuNum: video.DamuNum + 1})

	tx.Create(model.Damu{
		UserId:  userId,
		VideoId: req.VideoId,
		Modes:   req.Modes,
		Text:    req.Text,
		Stime:   req.Stime,
		Size:    req.Size,
		Color:   req.Color,
	})
	tx.Rollback()
	var damu model.Damu
	if damu.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "弹幕失败",
			"data": nil,
		})
		return
	}
	tx.Commit()
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "弹幕成功",
		"data": true,
	})
}
